﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.DataContract;
using MaxLeaf.Platform.DALImplementation;
using CommonsLib.DB;
using MaxLeaf.Platform.Functions;
using System.Data.SqlClient;

namespace MaxLeaf.Platform.BRL
{
    public class mUser_InfoBRL : BaseBRL
    { 

        public override bool AddValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!base.AddValid(dbcon, dal, model))
            {
                return false;
            }

            if (!CheckIs_OrderUser(dbcon, dal, model))
            {
                return false;
            }
            if (!CheckIs_DepAppro(dbcon, dal, model))
            {
                return false;
            }

            return true;
        }

        public override bool UpdateValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!base.UpdateValid(dbcon, dal, model))
            {
                return false;
            }

            if (!CheckIs_OrderUser(dbcon, dal, model))
            {
                return false;
            }
            if (!CheckIs_DepAppro(dbcon, dal, model))
            {
                return false;
            }

            return true;
        }

        private bool CheckIs_DepAppro(DBAccess dbcon, BaseDAL dal, object model)
        {
            mUser_InfoDataModel user = (mUser_InfoDataModel)model;
            if (!user.Is_DepAppro) return true;

            string strSql = "select top 1 * from mUser_Info  where UserID<>@UserID and CompanyID=@CompanyID and DepID=@DepID and DepAppro=1";
            using (System.Data.Common.DbDataReader dr = dbcon.GetDataFirstRow(strSql, new SqlParameter[]
                {
                    new SqlParameter("@UserID",user.Key)
                    ,new SqlParameter("@CompanyID",user.CompanyID)
                    ,new SqlParameter("@DepID",user.DepID)
                    
                }))
            {
                if (dr.Read())
                {
                    throw new MaxLeaf.Platform.Commons.MaxLeafException("已存在部门审核人");
                }
            }

            return true;
        }

        private bool CheckIs_OrderUser(DBAccess dbcon, BaseDAL dal, object model)
        {
            //mUser_InfoDataModel user = (mUser_InfoDataModel)model;
            //if (!user.Is_OrderUser) return true;

            //string strSql = "select top 1 * from mUser_Info where UserID<>@UserID and CompanyID=@CompanyID and DepID=@DepID and OrderUser=1";
            //using (System.Data.Common.DbDataReader dr = dbcon.GetDataFirstRow(strSql, new SqlParameter[]
            //    {
            //        new SqlParameter("@UserID",user.Key)
            //        ,new SqlParameter("@CompanyID",user.CompanyID)
            //        ,new SqlParameter("@DepID",user.DepID)
            //    }))
            //{
            //    if (dr.Read())
            //    {
            //        throw new MaxLeaf.Platform.Commons.MaxLeafException("已存在下单人");
            //    }
            //}

            return true;

        }
         
    }
}
